package com.sys.console.filter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

public class HttpUtil {

	public static PrintWriter getWriter(HttpServletResponse response){
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out;
	}
	
	/**
	 * 关闭printWriter对象,释放输出流
	 */
	public static void closeWriter(PrintWriter out){
		out.flush();
		out.close();
	}
	
	public static String decode(String str) {
		return decode(str, "utf-8");
	}
	
	public static String decode(String str, String decode) {
		String result = str;
		try {
			result = URLDecoder.decode(str, decode);
		} catch(Throwable e) {
		}
		return result;
	}
	
	/**
	 * 将请求参数全部打印到控制台
	 */
	@SuppressWarnings("rawtypes")
	public static String checkRequestParameter(HttpServletRequest request) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(request.getRequestURL() + "\n  ");
		Iterator parameters = request.getParameterMap().keySet()
				.iterator();
		while (parameters.hasNext()) {
			String parameter = (String)parameters.next();
			buffer.append(parameter + "--" + request.getParameter(parameter) + "\n    ");
		}
		return buffer.toString();
	}
	
	/**
	 * 将对字符串进行转义
	 * @param s
	 * @return
	 */
	public static String xssEncode(String s){
		//System.out.println(s);
		return s.replace("<", "").replace(">", "").replace("\"", "").replace("'", "");
		//return s.replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;").replace("'", "&apos;");

	}
	
	/**
	 * 将xss过滤之后的参数转义回去
	 * @param s
	 * @return
	 */
	public static String xssDecode(String s){
		return s.replace("&lt;", "<")
			.replace("&gt;", ">")
			.replace("&quot;", "\"")
			.replace("&apos;", "'")
			.replace("&amp;","&");
	}
	
	/**
	 * 清理HTML，去除不安全的
	 * @param html
	 * @return
	 */
	public static String cleanUnsafeHTML(String html){
		Whitelist wl = Whitelist.basic();
		wl.addTags("span");
		wl.addTags("h2");
		wl.addTags("h3");
		wl.addTags("h1");
		wl.addTags("embed");
		wl.addTags("img");
		wl.addTags("&nbsp;");
		wl.addAttributes("pre", "class");
		wl.addAttributes("img", "src", "alt", "width", "height");
		wl.addAttributes("embed", "src", "width", "height");
		wl.addAttributes(":all", "style");
		String safe = Jsoup.clean(html, wl);
		return safe;
	}
	
	/**
	 * 获取HTML中的文本，去除HTML标签
	 * @param html
	 * @return
	 */
	public static String parseHtmlText(String html){
		return Jsoup.parse(html).text();
	}
	/**
	 * 
	 */
/**
 * 
 * getIpAddress(这里用一句话描述这个方法的作用)    
 * (这里描述这个方法适用条件 – 可选)      
 * (这里描述这个方法的执行流程 – 可选)         
 * (这里描述这个方法的注意事项 – 可选)     
 * @param   name     
 * @param    
 * @return      返回值说明
 * @Exception 异常对象 
 * 创建人：wws  	日期：2015-7-30  下午1:41:00
 */
	public static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}